package com.devops.admin.graphql;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.devops.admin.model.DOSHelmChartRepository;
import com.devops.admin.model.TableResult;
import com.devops.admin.service.DOSHelmChartRepositoryService;
import com.devops.admin.vo.DOSPageVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName DOSHelmChartRepositoryDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 09:15
 * @Version 1.0
 **/
@DgsComponent
public class DOSHelmChartRepositoryDataFetcher {

    @Autowired
    private DOSHelmChartRepositoryService dosHelmChartRepositoryService;

    /**
     * @Author heyabo
     * @Description 添加仓库
     * @Date 09:21 2022/3/21
     * @Param [repository]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "添加仓库")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public DOSHelmChartRepository addRepository(@InputArgument DOSHelmChartRepository repository){
        dosHelmChartRepositoryService.saveRepository(repository);
        return repository;
    }

    /**
     * @Author heyabo
     * @Description 根据仓库的id更新仓库信息
     * @Date 09:29 2022/3/21
     * @Param [repository]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "根据仓库的id更新仓库信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean updateRepository(@InputArgument DOSHelmChartRepository repository){
        Assert.isTrue(ObjectUtil.isNotNull(repository.getId()),"仓库的id不能为空");
        return dosHelmChartRepositoryService.updateRepository(repository);
    }

    /**
     * @Author heyabo
     * @Description 根据id删除仓库信息
     * @Date 09:31 2022/3/21
     * @Param [repositoryId]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "根据id删除仓库信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean deleteRepository(@InputArgument Integer repositoryId){
        return dosHelmChartRepositoryService.removeById(repositoryId);
    }

    /**
     * @Author heyabo
     * @Description 多条件查询仓库
     * @Date 09:10 2022/3/21
     * @Param []
     * @return java.util.List<com.devops.admin.model.DOSHelmChartRepository>
     **/
    @DgsQuery
    @DOSLog(description = "多条件查询仓库")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public TableResult<DOSHelmChartRepository> getRepositorys(@InputArgument DOSHelmChartRepository repository, @InputArgument DOSPageVo pageVo){
        return dosHelmChartRepositoryService.getRepositorys(repository,pageVo);
    }
}
